﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KillBill.Business.Entities;
using KillBill.Business.Entities.ListEntities;
using KillBill.Common.View;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Products.IView;
using KillBill.Presentation.Modules.Products.Presenter;
using KillBill.Presentation.Modules.Products.ProductsServiceReference;

namespace KillBill.Presentation.Modules.Products.View
{
    public partial class ProductAddEditView : KillBillWinFormView, IProductAddEdit
    {
        private Product _product;

        public bool IsEditing { get; set; }

        public ProductAddEditPresenter Presenter { get; set; }

        public List<Provider> ProviderList
        {
            set
            {
                cmbProvider.DataSource = value;
                cmbProvider.DisplayMember = "Name";
                cmbProvider.ValueMember = "ProviderID";
            }
        }

        public Product Product
        {
            get
            {
                _product.ProviderID = (int)cmbProvider.SelectedValue;
                _product.Name = avtbName.Text;
                _product.Description = avtbDescription.Text;
                _product.LastPrice = Convert.ToDecimal(avtbLastPrice.Text);
                _product.Stock = Convert.ToInt32(avtbStock.Text);
                _product.StockMin = Convert.ToInt32(avtbStockMin.Text);
                _product.StockMax = Convert.ToInt32(avtbStockMax.Text);
                _product.IVAApplied = cbIVAApplied.Checked;
                if(!avtbICETax.AllowNull)
                    _product.ICETax = Convert.ToDouble(avtbICETax.Text);
                else
                    _product.ICETax = 0;
                return _product;
            }
            set
            {
                cmbProvider.SelectedValue = value.ProviderID;
                avtbName.Text = value.Name;
                avtbDescription.Text = value.Description;
                avtbLastPrice.Text = value.LastPrice.ToString();
                avtbStock.Text = value.Stock.ToString();
                avtbStockMin.Text = value.StockMin.ToString();
                avtbStockMax.Text = value.StockMax.ToString();
                cbIVAApplied.Checked = value.IVAApplied;
                avtbICETax.Text = value.ICETax.ToString();
                cbICEApplied.Checked = value.ICETax > 0;
                _product = value;
            }
        }

        public ProductAddEditView()
        {
            InitializeComponent();
            Presenter = new ProductAddEditPresenter(this);
            Presenter.GetProviders();
            IsEditing = false;
            _product = new Product();
            
            
        }

        public ProductAddEditView(Product product)
        {
            InitializeComponent();
            Presenter = new ProductAddEditPresenter(this);
            Presenter.GetProviders();
            IsEditing = true;
            Product = product;
            
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (avPanel1.IsValid)
            {
                Presenter.Save();
                Close();
            }
            else
                ShowMessage(Text, "La información ingresada no es válida", MessageOptions.Ok, MessageIcon.Exclamation);
        }

        private void cbICEApplied_CheckedChanged(object sender, EventArgs e)
        {
            if (!cbICEApplied.Checked)
                avtbICETax.Text = string.Empty;
            avtbICETax.AllowNull = !cbICEApplied.Checked;
            lblICETax.Enabled = cbICEApplied.Checked;
            avtbICETax.Enabled = cbICEApplied.Checked;
            
            
        }



        
    }
}
